package comment

import (
	
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/model/comment"
    commentReq "github.com/flipped-aurora/gin-vue-admin/server/model/comment/request"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type CommentsApi struct {}



// CreateComments 创建comments表
// @Tags Comments
// @Summary 创建comments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body comment.Comments true "创建comments表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /comments/createComments [post]
func (commentsApi *CommentsApi) CreateComments(c *gin.Context) {
	var comments comment.Comments
	err := c.ShouldBindJSON(&comments)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = commentsService.CreateComments(&comments)
	if err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:" + err.Error(), c)
		return
	}
    response.OkWithMessage("创建成功", c)
}

// DeleteComments 删除comments表
// @Tags Comments
// @Summary 删除comments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body comment.Comments true "删除comments表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /comments/deleteComments [delete]
func (commentsApi *CommentsApi) DeleteComments(c *gin.Context) {
	id := c.Query("id")
	err := commentsService.DeleteComments(id)
	if err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteCommentsByIds 批量删除comments表
// @Tags Comments
// @Summary 批量删除comments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /comments/deleteCommentsByIds [delete]
func (commentsApi *CommentsApi) DeleteCommentsByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	err := commentsService.DeleteCommentsByIds(ids)
	if err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateComments 更新comments表
// @Tags Comments
// @Summary 更新comments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body comment.Comments true "更新comments表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /comments/updateComments [put]
func (commentsApi *CommentsApi) UpdateComments(c *gin.Context) {
	var comments comment.Comments
	err := c.ShouldBindJSON(&comments)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = commentsService.UpdateComments(comments)
	if err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindComments 用id查询comments表
// @Tags Comments
// @Summary 用id查询comments表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query comment.Comments true "用id查询comments表"
// @Success 200 {object} response.Response{data=comment.Comments,msg=string} "查询成功"
// @Router /comments/findComments [get]
func (commentsApi *CommentsApi) FindComments(c *gin.Context) {
	id := c.Query("id")
	recomments, err := commentsService.GetComments(id)
	if err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:" + err.Error(), c)
		return
	}
	response.OkWithData(recomments, c)
}
// GetCommentsList 分页获取comments表列表
// @Tags Comments
// @Summary 分页获取comments表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query commentReq.CommentsSearch true "分页获取comments表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /comments/getCommentsList [get]
func (commentsApi *CommentsApi) GetCommentsList(c *gin.Context) {
	var pageInfo commentReq.CommentsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := commentsService.GetCommentsInfoList(pageInfo)
	if err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败:" + err.Error(), c)
        return
    }
    response.OkWithDetailed(response.PageResult{
        List:     list,
        Total:    total,
        Page:     pageInfo.Page,
        PageSize: pageInfo.PageSize,
    }, "获取成功", c)
}

// GetCommentsPublic 不需要鉴权的comments表接口
// @Tags Comments
// @Summary 不需要鉴权的comments表接口
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /comments/getCommentsPublic [get]
func (commentsApi *CommentsApi) GetCommentsPublic(c *gin.Context) {
    // 此接口不需要鉴权
    // 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
    commentsService.GetCommentsPublic()
    response.OkWithDetailed(gin.H{
       "info": "不需要鉴权的comments表接口信息",
    }, "获取成功", c)
}
